package com.qasystem.qa.core.service;

import com.qasystem.qa.core.base.BaseEnum;
import com.qasystem.qa.core.base.Result;
import com.qasystem.qa.core.common.CookieFactory;
import com.qasystem.qa.core.constants.BaseEnums;
import com.qasystem.qa.core.constants.Constants;
import com.qasystem.qa.core.scurity.ScurityUser;
import com.qasystem.qa.core.utils.Results;
import com.qasystem.qa.core.utils.UUIDCreater;
import com.qasystem.qa.system.dao.UserDAO;
import com.qasystem.qa.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
public class LoginService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private UserDAO userDAO;

    /**
     * 登陆以及存cookie
     * @param username 用户名
     * @param password 密码
     * @param response
     * @return
     */
    public Result login (String username, String password,HttpServletRequest request, HttpServletResponse response) {
        User tmpuser = new User(username, password);
        List<User> list = userDAO.select(tmpuser);
        Cookie[] cookies = null;
        /**for (User u : list) {
            System.out.println(u.getAccount());
        } **/
        if (!list.isEmpty()) {
            User user = list.get(0);
            String token = UUIDCreater.createToken();
            ScurityUser scurityUser = new ScurityUser(user, token);
            cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals(Constants.COOKIE_NAME)) {
                        String tokenUUID = cookie.getValue();
                        redisService.delete(tokenUUID);
                    }
                }
            }
            redisService.set(token, scurityUser);
            Cookie cookie = CookieFactory.getCookieInstance(Constants.COOKIE_NAME, token);
            response.addCookie(cookie);
            return Results.successWithData(username, BaseEnums.LOGIN_SUCCESS.code(), BaseEnums.LOGIN_SUCCESS.desc());
        } else {
            System.out.println(BaseEnums.LOGIN_FAILURE.code());
            System.out.println(BaseEnums.LOGIN_FAILURE.desc());
            return Results.failure(BaseEnums.LOGIN_FAILURE.code(), BaseEnums.LOGIN_FAILURE.desc());

        }
        //return Results.failure(BaseEnums.LOGIN_FAILURE.desc());

    }

    /**
     * 登出
     * @param request
     * @param response
     * @return
     */
    public Result logout (HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Boolean delete = false;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Constants.COOKIE_NAME)) {
                    String token = cookie.getValue();
                    System.out.println("token: " + token);
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    delete = redisService.delete(token);
                }
            }
            if (!delete) {
                return Results.failure("logout.failure", "用户未登陆");
            }
        }
        else {
            return Results.failure(BaseEnums.LOGOUT_FAILURE.code(), BaseEnums.LOGOUT_FAILURE.desc());
        }
        response.sendRedirect(request.getContextPath() + "/login");
        return Results.success(BaseEnums.LOGOUT_SUCCESS.code(), BaseEnums.LOGOUT_SUCCESS.desc());
    }
}
